import { reqGetDetai, reqAddOrUpdateShopcar } from "@/api"
import { getUserToken } from "@/utils/usertoken"
export default {
    namespaced: true,
    actions: {
        async getDetailInfo({ commit }, skuId) {
            let result = await reqGetDetai(skuId)
            if (result.code == 200) {
                commit("GETDETAILINFO", result.data)
            }
        },
        async addOrUpdateShopcar({ commit }, { skuId, skuNum }) {
            //async 返回的一定是Promise
            const result = await reqAddOrUpdateShopcar(skuId, skuNum)
            //判断是否成功 如果成功则返回一个非空 如果失败 然会一个失败的Promise
            if (result.code == 200) {
                return "ok"
            } else {
                return Promise.reject(new Error("fail"))
            }
        }
    },
    mutations: {
        GETDETAILINFO(state, data) {
            state.detailInfo = data
        }
    },
    state: {
        detailInfo: {},
        //用户临时id
        user_token: getUserToken()
    },
    getters: {
        categoryView(state) {
            return state.detailInfo.categoryView || {}
        },
        skuInfo(state) {
            return state.detailInfo.skuInfo || {}
        },
        spuSaleAttrList(state) {
            return state.detailInfo.spuSaleAttrList || [];
        }
    }
}